package com.ai.controller.security;


import com.ai.annotation.EnterPathParamLog;
import com.ai.common.CommonResponse;
import com.ai.common.exception.JeecgBootException;
import com.ai.entity.security.SysUser;
import com.ai.entity.security.SysUserVo;
import com.ai.entity.security.page.UserPage;
import com.ai.model.param.ModifyUserPasswordParam;
import com.ai.model.param.SysUserParam;
import com.ai.model.param.UserParam;
import com.ai.model.vo.SysUserModel;
import com.ai.service.security.ISysUserService;
import com.ai.common.vaild.AddGroup;
import com.ai.common.vaild.UpdateGroup;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户接口
 * @author chen
 * @since 2023-04-04 19:46:07
 */
@RestController
@RequestMapping("/user")
public class SysUserController {
    @Resource
    ISysUserService userService;

    @PostMapping("/login")
    @EnterPathParamLog
    public CommonResponse login(@RequestBody @Valid  UserParam userParam){
        return userService.login(userParam);
    }

    @ApiOperation("分页查询")
    @PostMapping("list")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('USER_MANAGE')")

    public IPage<SysUserModel> list(@RequestBody  UserPage page) {

        // 根据创建时间排序
//        QueryWrapper<SysUser> queryWrapper = page.getQuery(new QueryWrapper<>()).orderByDesc("create_time");
//        IPage<SysUser> iPage = userService.page(page, queryWrapper);

        return userService.getList(page);
    }

    @ApiOperation("删除用户")
    @PostMapping("delete/{id}")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('USER_MANAGE')")

    public CommonResponse<String> delete(@PathVariable(name = "id") String id) {

        return userService.deleteUser(Integer.valueOf(id));
    }

    @ApiOperation("新增用户")
    @PostMapping("add")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('USER_MANAGE')")

    public CommonResponse<String> add( @RequestBody @Validated({AddGroup.class}) SysUserParam user) {
        return userService.add(user);
    }

    @ApiOperation("更新用户")
    @PostMapping("update")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('USER_MANAGE')")

    public CommonResponse<String> update(@RequestBody @Validated({UpdateGroup.class}) SysUserParam user) {

        return userService.updateUser(user);
    }

    @ApiOperation("根据id获取指定用户")
    @PostMapping("selectUser/{id}")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('USER_MANAGE')")

    public CommonResponse selectOne(@PathVariable(name = "id") String id) {

        return userService.getUserInfoVo(id);

    }


    @ApiOperation("更新用户")
    @PostMapping("modifyPassword")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('USER_MANAGE')")

    public CommonResponse<String> modifyPassword(@RequestBody @Valid ModifyUserPasswordParam passwordParam) {

        return userService.modifyPassword(passwordParam);
    }
}

